#include "TeacherHome.h"
#include "ui_TeacherHome.h"
#include "QDebug"
#include "QSqlError"
#include "QMouseEvent"
#include "QPainter"
#include "QFileDialog"
#include "QBuffer"
#include "QMessageBox"
#include "QLineEdit"
#include "QAction"


TeacherHome::TeacherHome(const QString &TeaID,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TeacherHome)
{
    ui->setupUi(this);
    setWindowTitle("教师端");

    this->TeaID=TeaID;



    // 创建SQL查询对象
    QSqlQuery query;
    // 查询教师信息
       query.prepare("SELECT `姓名`, `教师工号`, `性别`,`出生日期`,`手机号`,`邮箱` FROM `教师表` WHERE `邮箱` = :teaID");
       query.bindValue(":teaID", TeaID);

       if (!query.exec()) {
           // 如果查询执行失败，弹出错误提示
           QMessageBox::critical(this, "查询错误", "无法获取教师信息: " + query.lastError().text());
           return;
       }

       if (query.next()) {
           // 获取查询结果
           QString name = query.value("姓名").toString();
           QString sex = query.value("性别").toString();
           QString Photo = query.value("手机号").toString();
           QString teacherId = query.value("教师工号").toString();
    }

    QPixmap pixmap(":/TeacherImage/12314.jpg");
    QPixmap chopped=pixmap.scaled(ui->photo->width(),ui->photo->height(),Qt::KeepAspectRatioByExpanding);
    QRect chopRect((chopped.width()-ui->photo->width())/2,0,ui->photo->width(),ui->photo->height());
    ui->photo->setPixmap(chopped.copy(chopRect));
    photo=*ui->photo->pixmap();


initClassTable();


}

void TeacherHome::SetTeacher(const QString &TeaID)
{
    this->TeaID=TeaID;
}

void TeacherHome::initClassTable()
{
    QTableWidgetItem *itemH0=new QTableWidgetItem(QIcon(":/TeacherImage/no.png"),"编号");
    QTableWidgetItem *itemH1=new QTableWidgetItem(QIcon(":/TeacherImage/no.png"),"学生人数");
    QTableWidgetItem *itemH2=new QTableWidgetItem(QIcon(":/TeacherImage/no.png"),"学习阶段");
    itemH0->setTextAlignment(Qt::AlignLeft);
    itemH1->setTextAlignment(Qt::AlignLeft);
    itemH2->setTextAlignment(Qt::AlignLeft);
    ui->tableWidget_3->setHorizontalHeaderItem(0,itemH0);
    ui->tableWidget_3->setHorizontalHeaderItem(1,itemH1);
    ui->tableWidget_3->setHorizontalHeaderItem(2,itemH2);

    int currentRow=0;
    while(currentRow<4)
    {
//        QTableWidgetItem *IdItem =new QTableWidgetItem(query.value("id").toString());

        //ui->tableWidget->insertRow(currentRow);
//        ui->tableWidget->setItem(currentRow,0,IdItem);
         ui->tableWidget_3->item(currentRow,0)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget_3->item(currentRow,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget_3->item(currentRow,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);


        QLineEdit *lineClass=new QLineEdit("268");
        lineClass->setFrame(false);
        lineClass->setFont(QFont("Microsoft YaHei UI", 12));
        lineClass->setAlignment(Qt::AlignCenter);
        lineClass->setReadOnly(true);

        QAction *tAct=new QAction(lineClass);

        tAct->setIcon(QIcon(":/TeacherImage/goin.png"));
        lineClass->addAction(tAct,QLineEdit::TrailingPosition);


        connect(tAct,&QAction::triggered,[this](){
            if(TeaInfo==nullptr)
            {
                TeaInfo= new TeacherMyStudentInfo;
                this->close();
                TeaInfo->show();
            }
            this->close();
            TeaInfo->show();
            //QMessageBox::information(this,"提示","学生表");
        });
        ui->tableWidget_3->setCellWidget(0,0,lineClass);

        currentRow++;


    }
}



TeacherHome::~TeacherHome()
{
    delete ui;
}



void TeacherHome::mouseMoveEvent(QMouseEvent *e)//在ui设置mouseTracking
{
    QPoint labelPos =ui->photo->mapFromParent(ui->widget_2->mapFromParent(e->pos()));
    //鼠标指向标签界面所在矩形框内
    if(ui->photo->rect().contains(labelPos)){
        ui->photo->setPixmap(QPixmap(":/TeacherImage/1.png").scaled(ui->photo->size()));
    }
    else{
        ui->photo->setPixmap(photo);
    }
}

void TeacherHome::insertAttendanceData(const QStringList &data)
{
    // 这里假设tableWidget_2已经在界面上设置好
       int row = ui->tableWidget_2->rowCount();
       ui->tableWidget_2->insertRow(row);

       // 遍历数据，将其插入到tableWidget_2的每一列
       for (int col = 0; col < data.size(); ++col) {
           ui->tableWidget_2->setItem(row, col, new QTableWidgetItem(data[col]));
       }
}

void TeacherHome::openAttebdanceWindow()
{
//    // 初始化 Attendance 对象
//     attendance = new Attendance(this);  // 将 TeacherHome 设置为父级

//     // 将教师 ID 设置给 Attendance 类
//     attendance->SetTeacher(TeaID);

//     // 调用 Attendance 类的 displayAttendance 方法来显示考勤信息
//     attendance->displayAttendance();
}

void TeacherHome::paintEvent(QPaintEvent *e)
{
    //绘画目标
    QPainter paint(this);
    QPixmap image(":/TeacherImage/bk.jpg");
    paint.drawPixmap(0,0,this->width(),this->height(),image);
}


void TeacherHome::on_tableWidget_2_cellActivated(int row, int column)
{
//    // 初始化 Attendance 对象
//     attendance = new Attendance(this);  // 将 TeacherHome 设置为父级

//     // 将教师 ID 设置给 Attendance 类
//     attendance->SetTeacher(TeaID);

//     // 调用 Attendance 类的 displayAttendance 方法来显示考勤信息
//     attendance->displayAttendance();
}
